package object2;

/**
 * 重写equals方法
 */
public class EqualsExercise01 {

    public static void main(String[] args) {
        /*
            判断两个E对象的内容是否相等，如果两个Person对象的各个属性值都一样，返回true 否则返回false
         */
        Person person1 = new Person("jack", 10, '男');
        Person person2 = new Person("jack", 20, '男');
        System.out.println(person1.equals(person2));//假
    }
}

class E{
    private String name;

    private int age;

    private char gender;

    public E(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age){
        this.age=age;
    }

    public char getGender(){
        return gender;
    }

    public void setGender(char gender){
        this.gender=gender;
    }

    //重写Object的equals方法
    public boolean equals(Object obj) {
        //如果比较的两个对象是同一个对象，直接返回true
        if (this == obj) {
            return true;
        }
        //类型判断
        if (obj instanceof E) { // 是E才判断
            //向下转型得到obj的各个属性
            E e = (E) obj;
            return e.name.equals(this.name) && e.age == this.age && e.gender == this.gender;
        }
        return false;
    }
}
